package com.mobilesales.android.services;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.reflect.TypeToken;
import com.mobilesales.android.model.exceptions.ServiceUnavailableException;
import com.mobilesales.android.model.orders.Order;
import com.mobilesales.android.model.orders.OrderStatus;

public class OrderServiceClient extends ServiceClientBase {

	public OrderServiceClient(Configuration configuration) {
		super(configuration);
	}
	
	public Order GetOrder(int orderId) throws ServiceUnavailableException{
		Order order = null;
		String url = getMethodURL() + "/" + String.valueOf(orderId);
		order = (Order) get(url, Order.class);
		return order;
	}
	
	public List<Order> GetOrderList() throws ServiceUnavailableException{
		List<Order> orderList = new ArrayList<Order>();
		List<Object> objectList = null;
		Type listType = new TypeToken<ArrayList<Order>>(){}.getType();
		String url = getMethodURL();
		objectList = getList(url, listType);
		if (objectList != null){
			for(Object obj : objectList){
				orderList.add((Order)obj);
			}
		} else 
			return null;
		return orderList;
	}
	
	public void StoreOrder(Order order) throws ServiceUnavailableException{
		String url = getMethodURL();
		post(url, order);
	}
	
	public OrderStatus GetOrderStatus(int orderStatusId) throws ServiceUnavailableException{
		OrderStatus orderStatus = null;
		String url = getMethodURL() + "/" + String.valueOf(orderStatusId);
		orderStatus = (OrderStatus) get(url, OrderStatus.class);
		return orderStatus;
	}
	
	public List<OrderStatus> GetOrderStatusList() throws ServiceUnavailableException{
		List<OrderStatus> orderStatusList = new ArrayList<OrderStatus>();
		List<Object> objectList = null;
		Type listType = new TypeToken<ArrayList<OrderStatus>>(){}.getType();
		String url = getMethodURL();
		objectList = getList(url, listType);
		if (objectList != null){
			for(Object obj : objectList){
				orderStatusList.add((OrderStatus)obj);
			}
		} else 
			return null;
		return orderStatusList;
	}

}
